package com.jsmy.marketing.base;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.jsmy.marketing.MPresenter;
import com.jsmy.marketing.MView;
import com.jsmy.marketing.R;
import com.jsmy.marketing.service.UpVersionService;
import com.jsmy.marketing.utils.DialogUtils;
import com.jsmy.marketing.utils.L;
import com.jsmy.marketing.utils.ToastUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.umeng.analytics.MobclickAgent;

import cn.jpush.im.android.api.event.LoginStateChangeEvent;
import io.reactivex.functions.Consumer;

/**
 * Created by PC_WLT on 2018/8/24.
 */

public abstract class BaseActivity extends BaseAppCompatActivity implements MView {
    protected Dialog mDialog;
    public RxPermissions rxPermissions;
    public MPresenter mPresenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        rxPermissions = new RxPermissions(this);
        mPresenter=new MPresenter(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onBaseCreate() {
        if (mDialog == null) {
            mDialog = new Dialog(this);
        }
    }

    @Override
    public void showProgress() {
        if (mDialog == null) {
            mDialog = new Dialog(this);
        }
        if (mDialog != null && !mDialog.isShowing())
            mDialog.show();
    }

    @Override
    public void hideProgress() {
        if (mDialog != null)
            mDialog.dismiss();
    }
    @Override
    public void showLoadFailMsg(String msg) {
        ToastUtil.showLong(this,msg);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
        hideProgress();
        mPresenter.cancle();
    }

    public void goToDownLoadService(final Context context, final String url){
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean){
                            Intent intent = new Intent(context, UpVersionService.class);
                            intent.putExtra("url", url);
                            startService(intent);
                        }else {
                            ToastUtil.showLong(context, "缺少权限，请授予SD卡的使用权限！");
                        }
                    }
                });
    }

    public void onEventMainThread(LoginStateChangeEvent event) {
        LoginStateChangeEvent.Reason reason = event.getReason();//获取变更的原因
        cn.jpush.im.android.api.model.UserInfo myInfo = event.getMyInfo();//获取当前被登出账号的信息
       L.e(TAG, "" + reason + "  " + myInfo);
        switch (reason) {
            case user_password_change:
                //用户密码在服务器端被修改
                break;
            case user_logout:
                //用户换设备登录
                DialogUtils.showLogOutDialog(this);
                break;
            case user_deleted:
                //用户被删除
                break;
        }
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        hideProgress();
        overridePendingTransition(R.anim.activity_left_exit, R.anim.activity_left_exit_hide);
    }

}
